SELECT (CAST(fenc.Suc_Id AS VARCHAR) + ' - ' + suc.Suc_Nombre) AS "Sucursal", 
	(CAST(fenc.Vendedor_id AS VARCHAR) + vnd.Vendedor_Nombre) AS "Vendedor", 
	YEAR(fenc.Factura_Fecha), MONTH(fenc.Factura_Fecha), DAY(fenc.Factura_Fecha), 
	(CAST(fdet.Articulo_Id AS VARCHAR) + ' - ' + art.Articulo_Nombre) AS "Articulo"  
FROM dbo.Factura_Detalle fdet
	INNER JOIN dbo.Factura_Encabezado fenc ON fdet.Factura_Id = fenc.Factura_Id
	INNER JOIN dbo.Articulo art ON fdet.Articulo_id = art.Articulo_Id
	INNER JOIN dbo.Sucursal suc ON fenc.Suc_Id = suc.Suc_Id 
	INNER JOIN dbo.Vendedor vnd ON fenc.Vendedor_id = vnd.Vendedor_Id
	
SELECT suc.* FROM [dbo].[Sucursal] suc ORDER BY  suc.Suc_Nombre

SELECT art.* FROM [dbo].[Articulo] art ORDER BY art.Articulo_Nombre
	
SELECT T1.Factura_Id AS "FACTURA_ID", COUNT(T1.Articulo_Id) AS "TOTAL_ARTICULOS", SUM(T1.TOTAL_CANTIDAD) AS "TOTAL_CANTIDADES", SUM(T1.TOTAL_PRECIO) AS "TOTAL_PRECIO" FROM 
(SELECT fenc.Factura_Id, fdet.Articulo_Id,
		SUM(fdet.Detalle_Cantidad) AS "TOTAL_CANTIDAD",
		SUM((fdet.Detalle_Precio_Unitario - fdet.Detalle_Descuento_monto) * fdet.Detalle_Cantidad) AS "TOTAL_PRECIO"
		FROM dbo.Factura_Detalle fdet
	INNER JOIN dbo.Factura_Encabezado fenc ON fdet.Factura_Id = fenc.Factura_Id
	WHERE fenc.Vendedor_Id = 10010726
		AND fenc.Suc_Id = 85
		--AND YEAR(fenc.Factura_Fecha) = YEAR(CURRENT_TIMESTAMP) AND MONTH(fenc.Factura_Fecha) = MONTH(CURRENT_TIMESTAMP)
		GROUP BY fenc.Factura_Id, fenc.Suc_Id, fenc.Vendedor_id, fenc.Factura_Fecha, fdet.Articulo_Id) T1
WHERE ((T1.Articulo_Id = '00006170' AND T1.TOTAL_CANTIDAD >= 1) OR 
	(T1.Articulo_Id = '00009799' AND T1.TOTAL_CANTIDAD >= 1) OR 
	(T1.Articulo_Id = 'S0001205' AND T1.TOTAL_CANTIDAD >= 1) OR 
	(T1.Articulo_Id = 'S0070018' AND T1.TOTAL_CANTIDAD >= 1) OR
	(T1.Articulo_Id = '00006484' AND T1.TOTAL_CANTIDAD >= 1))
GROUP BY T1.Factura_Id
	
	
SELECT * FROM Vendedor WHERE Vendedor_Id = 10010726; 
SELECT * FROM Sucursal WHERE Suc_Id = 85;





-- Meta con ID 29 asignada a usuario 230 - YESENIA BEATRIZ HERNANDEZ RAMOS en Sucursal 1
-- 2014-07-20 22:32:03 INFO  ProgressUpdater - Actualizando progreso con ID 57

SELECT T1.Factura_Id AS "FACTURA_ID", 
	T1.Factura_Numero AS "FACTURA_NUMERO",
	COUNT(T1.Articulo_Id) AS "TOTAL_ARTICULOS", 
	SUM(T1.TOTAL_CANTIDAD) AS "TOTAL_CANTIDADES", 
	SUM(T1.TOTAL_PRECIO) AS "TOTAL_PRECIO" 
FROM (SELECT fenc.Factura_Id, fenc.Factura_Numero,
		fdet.Articulo_Id, 
		SUM(fdet.Detalle_Cantidad) AS "TOTAL_CANTIDAD", 
		SUM((fdet.Detalle_Precio_Unitario - fdet.Detalle_Descuento_monto) * fdet.Detalle_Cantidad) AS "TOTAL_PRECIO" 
	FROM dbo.Factura_Detalle fdet 
		INNER JOIN dbo.Factura_Encabezado fenc ON fdet.Factura_Id = fenc.Factura_Id AND fenc.Suc_Id = fdet.Suc_Id AND fenc.Caja_Id = fdet.Caja_Id
	WHERE fenc.Vendedor_Id = 230 
		AND fenc.Suc_Id = 1 
		AND YEAR(fenc.Factura_Fecha) = YEAR(CURRENT_TIMESTAMP) 
		AND MONTH(fenc.Factura_Fecha) = MONTH(CURRENT_TIMESTAMP) 
	GROUP BY fenc.Factura_Id, fenc.Factura_Numero, fenc.Suc_Id, fenc.Vendedor_id, fenc.Factura_Fecha, fdet.Articulo_Id) T1 
WHERE ((T1.Articulo_Id = '0400041' AND T1.TOTAL_CANTIDAD >= 3)) 
GROUP BY T1.Factura_Id, T1.Factura_Numero
